package com.huhu.scenes;

import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;

import android.text.Editable;
import android.text.method.KeyListener;
import android.view.KeyEvent;
import android.view.View;

import com.huhu.GameActivity;
import com.huhu.base.BaseScene;
import com.huhu.manager.SceneManager;
import com.huhu.manager.SceneManager.SceneType;

public class LevelScene extends BaseScene implements IOnMenuItemClickListener {

	private MenuScene menuChildScene;
	private static final int BLOCK_1 = 1;
	private static final int BLOCK_2 = 2;
	private static final int BLOCK_3 = 3;
	private static final int BLOCK_4 = 4;
	private static final int BLOCK_5 = 5;
	private static final int BLOCK_6 = 6;

//	public static int maxLevel = 1;

	@Override
	public void createScene() {
		createBackground();
		createMenuChildScene();

	}

	@Override
	public void onBackKeyPressed() {
		SceneManager.getInstance().loadMenuScene(engine);

	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return SceneType.LEVEL;
	}

	@Override
	public void disposeScene() {
		// TODO Auto-generated method stub

	}

	private void createBackground() {
		final Sprite spriteBackground = new Sprite(0, 0,
				resourcesManager.stage_selection_bkggray_region, vbom);
//		spriteBackground.setScale(2f);
		this.setBackground(new SpriteBackground(spriteBackground));
		this.setBackgroundEnabled(true);
	}

	private void createMenuChildScene() {
		menuChildScene = new MenuScene(camera);
		final Sprite spriteSubBackground = new Sprite(0, 0,
				resourcesManager.stage_select_frame_region, vbom);
		spriteSubBackground.setPosition(
				GameActivity.CAMERA_WIDTH/2 - spriteSubBackground.getWidth() / 2,
				GameActivity.CAMERA_HEIGHT/2 - spriteSubBackground.getHeight() / 2);
		menuChildScene.attachChild(spriteSubBackground);

		final Sprite spriteTitle1 = new Sprite(0, 0,
				resourcesManager.stage_select_title1_region, vbom);
		spriteTitle1.setPosition(
				GameActivity.CAMERA_WIDTH/2 - spriteSubBackground.getWidth() / 2 + 150,
				GameActivity.CAMERA_HEIGHT/2 - spriteSubBackground.getHeight() / 2 + 75);
		menuChildScene.attachChild(spriteTitle1);
		final IMenuItem[] menuItems = new IMenuItem[6];
		for (int i = 0; i < SceneManager.maxLevel; i++) {
			menuItems[i] = new ScaleMenuItemDecorator(new SpriteMenuItem(i + 1,
					resourcesManager.stage_select_tiny_block_region, vbom),
					1.2f, 1);
		}

		for (int j = SceneManager.maxLevel; j < 6; j++) {
			menuItems[j] = new ScaleMenuItemDecorator(new SpriteMenuItem(j + 1,
					resourcesManager.stage_select_tiny_block_locked_region,
					vbom), 1.2f, 1);
		}

		// final IMenuItem item1 = new ScaleMenuItemDecorator(
		// new SpriteMenuItem(BLOCK_1,
		// resourcesManager.stage_select_tiny_block_region, vbom),
		// 1.2f, 1);
		// final IMenuItem item2 = new ScaleMenuItemDecorator(
		// new SpriteMenuItem(BLOCK_2,
		// resourcesManager.stage_select_tiny_block_locked_region, vbom),
		// 1.2f, 1);
		// final IMenuItem item3 = new ScaleMenuItemDecorator(
		// new SpriteMenuItem(BLOCK_3,
		// resourcesManager.stage_select_tiny_block_locked_region, vbom),
		// 1.2f, 1);
		// final IMenuItem item4 = new ScaleMenuItemDecorator(
		// new SpriteMenuItem(BLOCK_4,
		// resourcesManager.stage_select_tiny_block_locked_region, vbom),
		// 1.2f, 1);
		// final IMenuItem item5 = new ScaleMenuItemDecorator(
		// new SpriteMenuItem(BLOCK_5,
		// resourcesManager.stage_select_tiny_block_locked_region, vbom),
		// 1.2f, 1);
		// final IMenuItem item6 = new ScaleMenuItemDecorator(
		// new SpriteMenuItem(BLOCK_6,
		// resourcesManager.stage_select_tiny_block_locked_region, vbom),
		// 1.2f, 1);

		// menuChildScene.addMenuItem(item1);
		// menuChildScene.addMenuItem(item2);
		// menuChildScene.addMenuItem(item3);
		// menuChildScene.addMenuItem(item4);
		// menuChildScene.addMenuItem(item5);
		// menuChildScene.addMenuItem(item6);

		for (int i = 0; i < 6; i++) {
			menuChildScene.addMenuItem(menuItems[i]);
		}

		menuChildScene.buildAnimations();
		menuChildScene.setBackgroundEnabled(false);

		menuItems[0].setPosition(menuItems[2].getX() - 105,
				menuItems[2].getY() + 15);
		menuItems[1].setPosition(menuItems[2].getX(), menuItems[2].getY() + 15);
		menuItems[2].setPosition(menuItems[2].getX() + 105,
				menuItems[2].getY() + 15);
		menuItems[4].setPosition(menuItems[3].getX(), menuItems[3].getY() + 15);
		menuItems[5].setPosition(menuItems[3].getX() + 105,
				menuItems[3].getY() + 15);
		menuItems[3].setPosition(menuItems[3].getX() - 105,
				menuItems[3].getY() + 15);

		menuChildScene.setOnMenuItemClickListener(this);
		setChildScene(menuChildScene);
	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		SceneManager.saveActivte = false;
		switch (pMenuItem.getID()) {
		case BLOCK_1:
			if (BLOCK_1 <= SceneManager.maxLevel) {
				SceneManager.getInstance().loadGameScene(engine, BLOCK_1);
			}
			break;
		case BLOCK_2:
			if (BLOCK_2 <= SceneManager.maxLevel) {
				SceneManager.getInstance().loadGameScene(engine, BLOCK_2);
			}
			break;
		case BLOCK_3:
			if (BLOCK_3 <= SceneManager.maxLevel) {
				SceneManager.getInstance().loadGameScene(engine, BLOCK_3);
			}
			break;
		case BLOCK_4:
			if (BLOCK_4 <= SceneManager.maxLevel) {
				SceneManager.getInstance().loadGameScene(engine, BLOCK_4);
			}
			break;
		case BLOCK_5:
			if (BLOCK_5 <= SceneManager.maxLevel) {
				SceneManager.getInstance().loadGameScene(engine, BLOCK_5);
			}
			break;
		case BLOCK_6:
			if (BLOCK_6 <= SceneManager.maxLevel) {
				SceneManager.getInstance().loadGameScene(engine, BLOCK_6);
			}
			break;
		default:
			break;
		}

		return true;
	}
}
